/*
0 1 2 * * ? preload.js
*/
/*
 * @Author         : jiabao
 * @Description    :  插件脚本
 * @Date           : 2023-02-13 16:52:01
 * @FilePath       : \uTools_plugs\preload.js
 * 0 38 19 * * *
 */
// console.log("run time :")
// console.log(new Date().toLocaleTimeString('chinese', { hour12: false }))//18:45:50 

/**
 * @Description: 替换字符串指定索引的值
 * @return {string}
 */
String.prototype.replaceAt = function (index, replacement) {
    return this.slice(0, index) + replacement + this.slice(index + replacement.length);
}

/**
 * @Description: 把\转为/，或把/转为\	拷贝到剪贴板
 * @param {string} textIn 选中的文本
 * @return {string} textOut	转换后的文本
 */
function trans(textIn) {
    var textOut = textIn;
    var textLen = textIn.length;
    var pos = 0;
    try {
        if (textIn.indexOf('\\') >= 0 && textIn.indexOf('/') >= 0) {
            window.alert(" \\、/都有,无法判断出转化类型" + textIn.indexOf('\\') + "  " + textIn.indexOf('/'));
        }
        else if (textIn.indexOf('\\') >= 0) {	//	win转linux
            //window.alert("win转linux")
            while (pos < textLen && textIn.indexOf('\\', pos) >= 0) {
                pos = textIn.indexOf('\\', pos) + 1;
                textOut = textOut.replaceAt(pos - 1, '/');
            }
        }
        else if (textIn.indexOf('/') >= 0) {	//linux转win
            //window.alert("linux转win")
            while (pos < textLen && textIn.indexOf('/', pos) >= 0) {
                pos = textIn.indexOf('/', pos) + 1;
                textOut = textOut.replaceAt(pos - 1, '\\');
            }
        }
    }
    catch (err) {
        window.alert("err:" + err.message)
    }
    window.utools.copyText(textOut)	//拷贝到系统剪贴板
    return textOut
}

/**
 * @Description: 
 * @param {number} num	选中的要科学表达的数
 * @return {array} 返回可科学表达的方式
 */
function numHuman(num) {
    var res = [];
    var isNeg = false
    if (isNaN(num)) {	//不是数字
        console.log("not is num");
        res.push("not parse num")
        return res;
    }
    if (num == 0) {
        res.push(num)
        return res
    }
    if (num < 0) {
        num = -num;
        isNeg = true;
    }
    //是数字
    //K、W、M、G、T表示
    if (num < 1e3) {
        //return num;
        isNeg ? res.push(-num) : res.push(num)
    }
    else if (num >= 1e3 && num < 1e4) {
        //return (num / 1e3) + 'K';
        isNeg ? res.push(-(num / 1e3) + 'K') : res.push((num / 1e3) + 'K')
    }
    else if (num >= 1e4 && num < 1e6) {
        //return (num / 1e4) + 'W';
        isNeg ? res.push(-(num / 1e4) + 'W') : res.push((num / 1e4) + 'W')
    }
    else if (num >= 1e6 && num < 1e8) {
        //return (num / 1e6) + 'M';
        isNeg ? res.push(-(num / 1e6) + 'M') : res.push((num / 1e6) + 'M')
    }
    else if (num >= 1e8 && num < 1e10) {
        //return (num / 1e8) + 'G';
        isNeg ? res.push((num / 1e8) + 'G') : res.push((num / 1e8) + 'G')
    }
    else if (num >= 1e10 && num < 1e12) {
        //return (num / 1e10) + 'T';
        isNeg ? res.push(-(num / 1e10) + 'T') : res.push((num / 1e10) + 'T')
    }

    //科学计数法表示
    var p = Math.floor(Math.log(num) / Math.LN10);
    var n = num * Math.pow(10, -p);
    //return n+'e'+p
    isNeg ? res.push(-n + 'e' + p) : res.push(n + 'e' + p)

    var textShow = "";
    for (var i = 0; i < res.length; i++) {
        textShow += (res[i] + "\n")
    }
    window.alert(textShow)
    window.utools.copyText(textShow)
    return res
}

/**
 * @Description: 
 * @param {number} chars	选中的要计数的字符
 * @return {array} 返回选中的要计数的字符的个数描述
 */
function charCount(chars) {
    // 标点和中文
    var sTotal = 0;
    // 中文字判断
    var iTotal = 0;
    // 英文字母
    var eTotal = 0;
    // 数字判断
    var inum = 0;
    var engUpTotal = 0;
    var engLowTotal = 0;

    for (i = 0; i < chars.length; i++) {
        var c = chars.charAt(i);
        if (c.match(/[^\x00-\xff]/)) {
            //基本汉字
            if (c.match(/[\u4e00-\u9fa5]/)) {
                iTotal++;
            }
            //基本汉字补充
            else if (c.match(/[\u9FA6-\u9fcb]/)) {
                iTotal++;
            }
            sTotal++;
        }
        else {
            if (c.match(/[0-9]/)) {
                inum++;
            }
            else if (c.match(/[A-Z]/)) {
                engUpTotal++;
            }
            else if (c.match(/[a-z]/)) {
                engLowTotal++;
            }
            eTotal++;
        }
    }

    var textShow = "";
    textShow += ("汉字：" + iTotal + "\n")
    textShow += ("数字：" + inum + "\n")
    textShow += ("全角标点：" + (sTotal - iTotal) + "\n")
    textShow += ("半角标点：" + (eTotal - inum - engLowTotal - engUpTotal) + "\n")
    textShow += ("英文字母：" + (engUpTotal + engLowTotal) + "\n")
    textShow += ("大写字母：" + (engUpTotal) + "\n")
    textShow += ("小写字母：" + (engLowTotal) + "\n")
    textShow += ("-总字数-：" + (sTotal * 2 + eTotal))

    window.alert(textShow)
    window.utools.copyText(textShow)
    return textShow
}

/**
 * @Description: 脚本main
 * @return {void}
 */
window.exports = {
    "trans":	//对应的是 plugin.json 中的 features.code
    {
        mode: "none",	//用于无需 UI 显示，执行一些简单的代码
        args:
        {
            // 进入插件应用时调用
            enter: (action) => {
                // action = { code, type, payload }
                window.utools.hideMainWindow()	//隐藏输入框
                // do some thing
                var textIn = action.payload
                var textOut = trans(textIn)

                window.utools.outPlugin()

            }
        }
    },
    "numHuman":
    {
        mode: "none",	//用于无需 UI 显示，执行一些简单的代码
        args:
        {
            // 进入插件应用时调用
            enter: (action) => {
                // action = { code, type, payload }
                window.utools.hideMainWindow()	//隐藏输入框
                // do some thing
                var textIn = action.payload
                var textOut = numHuman(textIn)
                window.utools.outPlugin()
            }
        }
    },
    "charCount":
    {
        mode: "none",	//用于无需 UI 显示，执行一些简单的代码
        args:
        {
            // 进入插件应用时调用
            enter: (action) => {
                // action = { code, type, payload }
                window.utools.hideMainWindow()	//隐藏输入框
                // do some thing
                var textIn = action.payload
                var textOut = charCount(textIn)
                window.utools.outPlugin()
            }
        }
    }
}